package com.yuji.aacar.net.config;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.X509TrustManager;


/**
 * <li>Package:consumer.ttpc.com.httpmodule.converterfactory  </li>
 * <li>Author: yehaijian  </li>
 * <li>Date: 2017/5/2  </li>
 * <li>Description:  </li>
 */
public class HttpCoreDES {
    private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};

    /**
     * des加密
     * @param encryptString
     * @param encryptKey
     * @return
     * @throws Exception
     */
    public static byte[] encryptDES(String encryptString, String encryptKey)
            throws Exception {
        //System.out.println("willencryptedData with UTF-8 encoding =" + parseByte2HexStr(encryptString.getBytes("UTF-8")));
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));
        //System.out.println("didencryptedData =" + parseByte2HexStr(encryptedData));
        return encryptedData;
    }

    /**
     * des加密以后再base64转码
     * @param encryptString
     * @return
     * @throws Exception
     */
    public static String encryptDESwithBase64(String encryptString) throws Exception {
        //return new String(Base64.encodeBase64(encryptDES(encryptString, encryptKey)), "utf-8");
//        return new String(Base64.encode(encryptDES(encryptString, getKey()), Base64.NO_WRAP), "utf-8");
        return encryptString;
    }

    /**
     * DES解密
     * @param encryptedData
     * @param decryptKey
     * @return
     * @throws Exception
     */
    public static String decryptDES(byte[] encryptedData, String decryptKey)
            throws Exception {
        //System.out.println("willdecryptedData =" + parseByte2HexStr(encryptedData));
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes("UTF-8"), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
        byte decryptedData[] = cipher.doFinal(encryptedData);
        //System.out.println("diddecryptedData with UTF-8 encoding =" + parseByte2HexStr(decryptedData));
        String decryptedString = new String(decryptedData, "UTF-8");
        //System.out.println("diddecryptedString with UTF-8 encoding =" + decryptedString);
        return decryptedString;
    }

    /**
     * 先base64解码再de解密
     * @param encryptedString
     * @return
     * @throws Exception
     */
    public static String decryptDESwithBase64(String encryptedString) throws Exception {
        //byte[] encryptedData = Base64.decodeBase64(encryptedString.replaceAll(" ", "+").getBytes());
//        byte[] encryptedData = Base64.decode(encryptedString.replaceAll(" ", "+").getBytes(), Base64.NO_WRAP);
//        return decryptDES(encryptedData, getKey());
        return encryptedString;
    }


    /**
     * 将byte数组转换为表示16进制值的字符串， 如：byte[]{8,18}转换为：0813， 和public static byte[]
     * hexStr2ByteArr(String strIn) 互为可逆的转换过程
     *
     * @param arrB 需要转换的byte数组
     * @return 转换后的字符串
     * @throws Exception 本方法不处理任何异常，所有异常全部抛出
     */
    public static String parseByte2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        // 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    public static X509TrustManager  getX509TrustManager(){
        final X509TrustManager trustAllCert =
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)  {
                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)  {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                };

                return trustAllCert;
    }
}
